package com.uam.web.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.mapbox.geojson.Point;
import com.mapbox.turf.TurfConstants;
import com.mapbox.turf.TurfMeasurement;
import com.uam.db.model.entity.RoutQueryModel;
import com.uam.web.common.vo.PointVo;
import org.locationtech.jts.geom.Coordinate;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author Administrator
 */
public class CurrentListGroup {

    /**
     * 按照连续的数字分组
     *
     * @param numbers
     * @return
     */
    public static List<List<Integer>> groupConsecutiveNumbers(List<Integer> numbers) {
        List<List<Integer>> groups = new ArrayList<>();
        if (numbers.isEmpty()) {
            return groups;
        }
        // 先排序
        Collections.sort(numbers);
        List<Integer> currentGroup = new ArrayList<>();
        currentGroup.add(numbers.get(0));
        for (int i = 1; i < numbers.size(); i++) {
            int currentNumber = numbers.get(i);
            int previousNumber = numbers.get(i - 1);

            // 如果当前数字与前一个数字连续，则将其添加到当前分组中
            if (currentNumber - previousNumber == 1) {
                currentGroup.add(currentNumber);
            } else {
                // 否则，将当前分组添加到结果列表中，并创建一个新的分组
                groups.add(new ArrayList<>(currentGroup));
                currentGroup.clear();
                currentGroup.add(currentNumber);
            }
        }
        // 添加最后一个分组
        groups.add(new ArrayList<>(currentGroup));

        return groups;
    }

    public static boolean isInRectangle(List<Integer> index, List<PointVo> listPoint, String newRectgle) {
        String[] rectangleArray = newRectgle.split(";");
        String[] minArray = rectangleArray[0].split("\\,");
        String[] maxArray = rectangleArray[1].split("\\,");
        int row = index.size();
        for (int i = 0; i < row; i++) {
            PointVo indexKey = listPoint.get(index.get(i));
            double lat = Double.valueOf(indexKey.getPoints().get(0).toString());
            double lng = Double.valueOf(indexKey.getPoints().get(1).toString());
            boolean flag = GeoUtil.isInRectangleArea(lat, lng, Double.valueOf(minArray[0]), Double.valueOf(maxArray[0]), Double.valueOf(minArray[1]), Double.valueOf(maxArray[1]));
            if (!flag) {
                return false;
            }
        }
        return true;
    }

    public static boolean isInRectangle(JSONArray listPoint, String newRectgle) {
        String[] rectangleArray = newRectgle.split(";");
        String[] minArray = rectangleArray[0].split("\\,");
        String[] maxArray = rectangleArray[1].split("\\,");
        int row = listPoint.size();
        for (int i = 0; i < row; i++) {
            JSONArray indexKey = listPoint.getJSONObject(i).getJSONArray("position");
            double lat = indexKey.getDouble(0);
            double lng = indexKey.getDouble(1);;
            boolean flag = GeoUtil.isInRectangleArea(lat, lng, Double.valueOf(minArray[0]), Double.valueOf(maxArray[0]), Double.valueOf(minArray[1]), Double.valueOf(maxArray[1]));
            if (!flag) {
                return false;
            }
        }
        return true;
    }

    private static Point getPoint(PointVo point){
        double lon = Double.valueOf(point.getPoints().get(0).toString());
        double lat = Double.valueOf(point.getPoints().get(1).toString());
        return Point.fromLngLat(lon,lat);
    }

    private static double getPointDouble(PointVo point,int index){
        return Double.valueOf(point.getPoints().get(index).toString());
    }

    public static List<PointVo> newPointsInfo(List<Integer> indexList, List<PointVo> listPoint,int distance) {



        int row=indexList.size();
        int startIndex=indexList.get(0)-1;
        int endIndex=indexList.get(row-1)+2;

        List<PointVo> listOld =  new ArrayList<>();
        listPoint.subList(startIndex,endIndex).forEach(a->{
            PointVo pointVo=new PointVo();
            pointVo.setConflictPoint(a.isConflictPoint());
            pointVo.setPoints(new ArrayList<>(a.getPoints()));
            listOld.add(pointVo);
        });

        StringBuffer sbOld=new StringBuffer();

        List<PointVo> pointVos= listPoint.subList(startIndex,endIndex);
        List<Coordinate> points=new ArrayList<>();
        pointVos.forEach(a->{
            Coordinate coordinate=new Coordinate(getPointDouble(a,0),getPointDouble(a,1));
            points.add(coordinate);
        });
        List<Point> pointsTemp=KeepPointDateInfo.dataInfo(points,distance+10);

        for(int i=startIndex;i<endIndex;i++){
            Point coordinate = pointsTemp.get(i-startIndex);
            PointVo s=  listPoint.get(i);
            s.getPoints().set(0,coordinate.coordinates().get(0));
            s.getPoints().set(1,coordinate.coordinates().get(1));
        }

        listPoint.forEach(a->{
            sbOld.append(getPointDouble(a,0));
            sbOld.append(",");
            sbOld.append(getPointDouble(a,1));
            sbOld.append(";");
        });
        sbOld.deleteCharAt(sbOld.length()-1);


        StringBuffer sb=new StringBuffer();
            listOld.forEach(a->{
            sb.append(getPointDouble(a,0));
            sb.append(",");
            sb.append(getPointDouble(a,1));
            sb.append(";");
        });

        sb.deleteCharAt(sb.length()-1);
        System.out.println("计算前--"+sb.toString());
        System.out.println("计算后--"+sbOld.toString());
        return listOld;
    }

}
